package com.yzh.ios6;

import java.io.*;

/**
 * @author 于振晗
 * @title: ObjectInputStreamTest
 * @description: TODO 对象流
 * @date 2020年09月07日
 * @path com.yzh.ios6
 */
public class ObjectInputStreamTest {
    public static void main(String[] args) throws IOException, ClassNotFoundException {
        write();
        read();
    }

    public static void read() throws IOException, ClassNotFoundException {
        BufferedInputStream bis = new BufferedInputStream(new FileInputStream("d:/test5.txt"));
        ObjectInputStream dis = new ObjectInputStream(bis);
        System.out.println(dis.readInt());
        System.out.println(dis.readDouble());
        System.out.println(dis.readChar());
        System.out.println(dis.readBoolean());
        System.out.println(dis.readObject());
        bis.close();

    }
    public static void write() throws IOException {
        BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("d:/test5.txt"));
        ObjectOutputStream dos = new ObjectOutputStream(bos);
        dos.writeInt(1);
        dos.writeDouble(5.5);
        dos.writeChar('A');
        dos.writeBoolean(true);
        dos.writeObject(new Student("小久",8));
        dos.close();
    }
}


class Student implements Serializable{
    private static final long serialVersionUID = 8183406416692229948L;
    private String name;
    private int age;

    public Student(String name, int age) {
        this.name = name;
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }
}